/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lovebomb.jfilesearch;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.print.attribute.AttributeSet;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.DocumentEvent.EventType;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;

import com.itextpdf.text.Font;

import lovebomb.jfilesearch.engine.FileSearchEngine;

/**
 *
 * @author user
 */
public class SearchForm extends javax.swing.JFrame {

	private FileSearchEngine fse = FileSearchEngine.getInstance( );
	private Thread t = null;
	
	
    /**
     * Creates new form SearchForm
     */
    public SearchForm() {
        initComponents();
        this.init();
    }

    private void init(){
    	this.tfFolder.setEditable(false); 
    	this.lbResult.setEditable(false);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbStatus = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        tfFolder = new javax.swing.JTextField();
        btFolder = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lbResult = new javax.swing.JEditorPane("text/html" , "");
        tfSearchWord = new javax.swing.JTextField();
        btSearch = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        tfSFolder = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfSFile = new javax.swing.JTextField();
        btStop = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        lbStatus.setText("Start");
        getContentPane().add(lbStatus, java.awt.BorderLayout.PAGE_END);

        tfFolder.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btFolder.setText("폴더");
        btFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFolderActionPerformed(evt);
            }
        });

        tfSearchWord.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tfSearchWord.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				if( arg0.getKeyCode() == KeyEvent.VK_ENTER){
					start();
				}
				
			}
		});

        btSearch.setText("검색");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        jLabel2.setText("검색어");

        tfSFolder.setBackground(new java.awt.Color(0, 0, 0));
        tfSFolder.setForeground(new java.awt.Color(255, 255, 0));
        tfSFolder.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("검색폴더");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("검색파일");

        tfSFile.setBackground(new java.awt.Color(0, 0, 0));
        tfSFile.setForeground(new java.awt.Color(255, 255, 0));
        tfSFile.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btStop.setText("중지");
        btStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btStopActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btFolder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tfSFolder, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(tfFolder, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
                                .addGap(24, 24, 24)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tfSearchWord, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(tfSFile))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btStop, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(btSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfSearchWord, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(btSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btFolder, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfFolder, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfSFolder, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfSFile, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)))
                    .addComponent(btStop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 474, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        

        jScrollPane2.setViewportView(lbResult);
        
        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jMenu1.setText("메뉴");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("도움말");
        jMenuBar1.add(jMenu2);

        //setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void startSetting(){
    }
    
    public void endSetting(){
    }

    
    public void setFolderConsole( String strMessage){
   		this.tfSFolder.setText("  "+ strMessage);    		
    }
    
    public void setFileConsole( String strMessage){
   		this.tfSFile.setText("  "+ strMessage);    		
    }    
    
    public void setResultColsole( String strFile){
    	try {
    		SimpleAttributeSet sas = new SimpleAttributeSet();
        	this.lbResult.getDocument().insertString(lbResult.getDocument().getLength(), "<a href='"+strFile+"'>"+strFile+"</a>",  (AttributeSet)sas);
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
    
    private void start(){
    	
    	if( this.checkSearchWord() == true){

    		this.lbResult.setText("");
	    	fse.init(this, this.tfFolder.getText(), this.tfSearchWord.getText());
	    	this.startSetting(); 	
	    	t = new Thread(fse);
	    	t.start();
    	}
    	else{
    		
    	}	
    }
    
    private boolean checkSearchWord(){
    	if( new File( this.tfFolder.getText()).isDirectory() == false){
    		JOptionPane.showMessageDialog( this, "폴더를 선택해주세요");
    		return false;
    	}
    	else if( this.tfSearchWord.getText().trim().equals("") == true){
    		JOptionPane.showMessageDialog(this, "검색어를 넣어 주세요");
    		return false;
    	}    	
    	return true;    	
    }
    
    private void btFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFolderActionPerformed
    	JFileChooser jrc = new JFileChooser();
    	jrc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    	if( jrc.showOpenDialog( this) == JFileChooser.APPROVE_OPTION){
    		this.tfFolder.setText( jrc.getSelectedFile().getPath());
    	}
    }//GEN-LAST:event_btFolderActionPerformed

    // 검색버튼
    private void btSearchActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
    	start();
    	
    }//GEN-LAST:event_btSearchActionPerformed


    //멈춤 버튼
    private void btStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btStopActionPerformed
    	if( t != null){
	        t.stop();
	        endSetting();
    	}
    }//GEN-LAST:event_btStopActionPerformed

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SearchForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SearchForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SearchForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SearchForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SearchForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btFolder;
    private javax.swing.JButton btSearch;
    private javax.swing.JButton btStop;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JEditorPane lbResult;
    private javax.swing.JLabel lbStatus;
    private javax.swing.JTextField tfFolder;
    private javax.swing.JTextField tfSFile;
    private javax.swing.JTextField tfSFolder;
    private javax.swing.JTextField tfSearchWord;
    // End of variables declaration//GEN-END:variables
}
